package ru.incoremedia.gwttest.core.client.component.ajaxloaderindicator;

import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.PopupPanel;
import com.google.gwt.user.client.ui.UIObject;


public class AjaxLoaderIndicator {

    private static PopupPanel p = new PopupPanel();

    private static int count = 0;

    static { 
        Image image = new Image(AjaxLoaderIndicatorResourceBundle.INSTANCE.ajaxIndicator());
        p.add(image);
        p.setStyleName(AjaxLoaderIndicatorResourceBundle.INSTANCE.cssResources().noborder());
    }

    public static void showModal() {
        p.setPopupPosition(15, 35);
        p.setModal(true);
        p.show();
        count++;
    }

    public static void show() {
        p.setPopupPosition(15, 35);
        p.setModal(false);
        p.show();
        count++;
    }

    public static void showModalRelativeTo(UIObject object) {
        p.setModal(true);
        p.showRelativeTo(object);
        count++;
    }

    public static void showRelativeTo(UIObject object) {        
        p.setModal(false);
        p.showRelativeTo(object);
        count++;
    }

    public static void hide() {
        count--;

        if(count < 0) {
            count = 0;
        }

        if(count == 0) {
            p.hide();
        }
    }

}
